/**
 * Project Name:九色鹿
 * File Name:Md5Util.java
 * Package Name:com.fxp.project.jiuselu.commons.md5
 * Date:2021-08-26 12:18 上午
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.commons.md5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * ClassName: Md5Util<br/>
 * Function: MD5的工具类<br/>
 * Date: 2021-08-26 12:18 上午<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0.0
 * @see
 * @since JDK 1.8
 */
public class Md5Util {
    public static String md5(String input) {
        char[] hexDigits = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(input.getBytes());
            byte[] encryptStr = messageDigest.digest();
            char[] str = new char[32];
            int k = 0;
            for (int i = 0; i < 16; i++) {
                byte byte0 = encryptStr[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xF];
                str[k++] = hexDigits[byte0 & 0xF];
            }
            return new String(str);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }
}
